package org.cug.controller;

import lombok.extern.slf4j.Slf4j;
import org.cug.pojo.CourseDesign;
import org.cug.pojo.Result;
import org.cug.service.CoursedesignService;
import org.cug.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/courseDesign")
@Slf4j
/**
 * 该控制器用于处理课程设计相关的代码的增删改查，操纵的数据库为，course_desgin
 * state:已测验
 */
public class CoursedesignController {
    @Autowired
    private CoursedesignService coursedesignService;

    @GetMapping
    public Result list(@RequestParam(required = false) Short semester){
        log.info("查询所有课程设计信息");
        //本地线程获取
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        List<CourseDesign> coursedesigns=coursedesignService.list(userId,semester);
        return Result.success(coursedesigns);
    }

    @PostMapping
    public Result add(@RequestBody CourseDesign coursedesign){
        log.info("增加课程设计信息{}",coursedesign);
        Map<String,Object> claims= ThreadLocalUtil.get();
        System.out.println(123);
        Integer userId = (Integer) claims.get("id");
        coursedesign.setUserId(userId);
        coursedesignService.add(coursedesign);
        return Result.success();
    }

    @DeleteMapping("/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("删除课程设计信息{}",ids);
        coursedesignService.delete(ids);
        return Result.success();
    }
    //根据id进行查询
    @GetMapping("/{id}")
    public Result selectid(@PathVariable Integer id) {
        log.info("查询id{}",id);
        CourseDesign coursedesign=coursedesignService.selectid(id);
        return Result.success(coursedesign);
    }
    //通过id进行更改
    @PutMapping
    public Result alterbyid(@RequestBody CourseDesign coursedesign){
        log.info("修改课程设计信息{}",coursedesign);
        coursedesignService.alterbyid(coursedesign);
        return Result.success();
    }
}
